import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:jetour_line/base_widget/base_scaffold.dart';
import 'package:jetour_line/my_colors.dart';
import 'package:jetour_line/network/jetour_http.dart';
import 'package:jetour_line/utils/aha_utils.dart';
import 'package:jetour_line/utils/extension_int.dart';

import '../../base_widget/base_image.dart';
import '../../network/aha_http.dart';

class JoinGroupScreen extends StatefulWidget {
  const JoinGroupScreen({super.key});

  @override
  State<JoinGroupScreen> createState() => _JoinGroupScreenState();
}

class _JoinGroupScreenState extends State<JoinGroupScreen> {
  String? code;
  bool showClear = false;
  TextEditingController textEditingController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return BaseScaffold(
        backgroundColor: Colors.white,
        title: '加入组队',
        body: Container(
          width: double.infinity,
          height: double.infinity,
          child: Column(
            children: [
              SizedBox(
                height: 80.px,
              ),
              ClipRRect(
                borderRadius: BorderRadius.all(Radius.circular(4.px)),
                child: Container(
                  alignment: Alignment.center,
                  width: 536.px,
                  height: 138.px,
                  color: MyColors.colorF1F1F1,
                  child: Row(
                    children: [
                      SizedBox(
                        width: showClear?72.px:32.px,
                      ),
                      Expanded(
                        child: TextField(
                          controller: textEditingController,
                          textAlign: TextAlign.center,
                          keyboardType: TextInputType.number,
                          onChanged: (value) {
                            code = value;
                            if(code != null && code!.length>0){
                              showClear = true;
                            }else {
                              showClear = false;
                            }
                            setState(() {

                            });
                          },
                          style: TextStyle(
                              fontSize: 58.px, color: MyColors.color1A1A1A,fontWeight: FontWeight.w600),
                          decoration:  InputDecoration(
                            border: InputBorder.none,
                            hintText: '输入邀请码',
                            hintStyle: TextStyle(fontSize: 42.px,color: MyColors.color666666, fontWeight: FontWeight.normal),
                            isDense: true,
                            contentPadding: EdgeInsets.fromLTRB(8.px, 0, 0, 0),
                          ),
                        ),
                      ),
                      if (showClear)
                        GestureDetector(
                          onTap: () {
                            code = '';
                            showClear = false;
                            textEditingController.clear();
                            setState(() {

                            });
                          },
                          child: Container(
                            color: Colors.transparent,
                            padding: EdgeInsets.only(left: 32.px,right: 32.px,top: 20.px,bottom: 20.px),
                            child: ClipRRect(
                              borderRadius:
                                  BorderRadius.all(Radius.circular(20.px)),
                              child: Opacity(
                                opacity: 0.8,
                                child: Container(
                                  alignment: Alignment.center,
                                  color: MyColors.color26282D,
                                  width: 40.px,
                                  height: 40.px,
                                  child: BaseImage(
                                    image: 'lsgj_btn_closewh',
                                    width: 20.px,
                                    height: 20.px,
                                  ),
                                ),
                              ),
                            ),
                          ),
                        ),

                    ],
                  ),
                ),
              ),
              SizedBox(
                height: 48.px,
              ),
              Text(
                '请输入好友提供的组队邀请码',
                style: TextStyle(fontSize: 28.px, color: MyColors.color666666),
              ),
              SizedBox(
                height: 80.px,
              ),
              GestureDetector(
                onTap: joinGroup,
                child: ClipRRect(
                  borderRadius: BorderRadius.all(Radius.circular(40.px)),
                  child: Container(
                    alignment: Alignment.center,
                    color: MyColors.appColor,
                    width: 536.px,
                    height: 80.px,
                    child: Text(
                      '确定',
                      style: TextStyle(
                          fontSize: 30.px,
                          color: MyColors.white,
                          fontWeight: FontWeight.w600),
                    ),
                  ),
                ),
              )
            ],
          ),
        ));
  }
  void joinGroup(){
    if(code!= null && code!.isNotEmpty){
      Map<String, dynamic>? params = Map();
      params['code'] = code;
      AhaHttp().post(AhaRouter.JoinByCode,data:params).then((value) {
        AhaUtils.showTxt("您已成功加入组队");
        Future.delayed(const Duration(milliseconds: 1500), () {
          Navigator.pop(context);
          // 延迟执行的代码
        });
      });
    }else{
      AhaUtils.showTxt("请输入邀请码");
    }
  }
}